﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;


namespace SportingGoodsExhibitionSystem
{
    /// <summary>
    /// updatePassword.xaml 的交互逻辑
    /// </summary>
    public partial class updatePassword : Page
    {
        public updatePassword()
        {
            InitializeComponent();
        }

        int stuNum;
        string password;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(oldPassword.Password.ToString().Equals(password))
            {
                SqlConnection con = new SqlConnection(strCon);
                con.Open();
                SqlCommand cmd = new SqlCommand("update [student] set passWord=@num where studentNumber=@id1", con);
                cmd.Parameters.Add("@id1", SqlDbType.Int, 10).Value = stuNum;
                cmd.Parameters.Add("@num", SqlDbType.VarChar, 50).Value = newPassword.Password;
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("密码修改成功");
                oldPassword.Password = "";
                newPassword.Password = "";
            }
            else
            {
                MessageBox.Show("原密码输入错误");
            }
        }

        string strCon = Properties.Resources.strCON;

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from studentInfo where studentStatus=@num", con);
            cmd.Parameters.Add("@num", SqlDbType.Int, 10).Value = 1;
            SqlDataAdapter read = new SqlDataAdapter(cmd);
            //DataTable dt = new DataTable();
            DataSet dataset = new DataSet();
            read.Fill(dataset);
            DataTable table = dataset.Tables[0];
            DataRowCollection rows = table.Rows;
            DataRow row = rows[0];
            stuNum = Convert.ToInt32(row["studentNumber"].ToString());
            cmd = new SqlCommand("select * from student where studentNumber=@num", con);
            cmd.Parameters.Add("@num", SqlDbType.Int, 10).Value = stuNum;
            SqlDataAdapter read1 = new SqlDataAdapter(cmd);
            //DataTable dt = new DataTable();
            DataSet dataset1 = new DataSet();
            read1.Fill(dataset1);
            DataTable table1 = dataset1.Tables[0];
            DataRowCollection rows1 = table1.Rows;
            DataRow row1 = rows1[0];
            password = row1["passWord"].ToString();
            con.Close();
        }
    }
}
